﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lottery.MediaData.Core {
    public static class WZHelper {
        private static WZ GetLeft(List<WZ> data, int index) {
            WZ result = null;
            for (int i = index - 1; i >= 0; i--) {
                var item = data[i];
                if (item.Three == "" && item.One == "" && item.Zero == "") continue;

                result = item;
                break;
            }

            if (result == null) {
                for (int i = data.Count - 1; i > index; i--) {
                    var item = data[i];
                    if (item.Three == "" && item.One == "" && item.Zero == "") continue;

                    result = item;
                    break;
                }
            }

            return result;
        }

        private static WZ GetRight(List<WZ> data, int index) {
            WZ result = null;
            for (int i = index + 1; i < data.Count; i++) {
                var item = data[i];
                if (item.Three == "" && item.One == "" && item.Zero == "") continue;

                result = item;
                break;
            }

            if (result == null) {
                for (int i = 0; i < index; i++) {
                    var item = data[i];
                    if (item.Three == "" && item.One == "" && item.Zero == "") continue;

                    result = item;
                    break;
                }
            }

            return result;
        }

        public static CellData Gen(List<WZ> data) {
            var result = new CellData();

            // 清空以前的数据
            for (int col = 0; col < result.ColCount; col++)
                for (int row = 0; row < result.RowCount; row++)
                    result[col, row] = string.Empty;


            for (int i = 0; i < data.Count; i++) {
                var item = data[i];
                var col = i;

                result[col, 0] = item.D ?? string.Empty;
                result[col, 1] = item.Z ?? string.Empty;
                result[col, 2] = item.X ?? string.Empty;

                // 与预测号相同
                result[col, 3] = item.PreNumber ?? string.Empty;
                if (item.PreText == "大" || item.PreText == "首")
                    result[col, 4] = item.PreNumber ?? string.Empty;
                if (item.PreText == "中" || item.PreText == "次")
                    result[col, 5] = item.PreNumber ?? string.Empty;
                if (item.PreText == "小" || item.PreText == "末")
                    result[col, 6] = item.PreNumber ?? string.Empty;

                // 与预测号相邻
                WZ left = GetLeft(data, i);
                WZ right = GetRight(data, i);

                var s = left.PreText;
                if (item.PreText != s) {
                    if (s == "大" || s == "首") {
                        result[col, 8] = item.D ?? string.Empty;
                    } else if (s == "中" || s == "次") {
                        result[col, 9] = item.Z ?? string.Empty;
                    } else if (s == "小" || s == "末") {
                        result[col, 10] = item.X ?? string.Empty;
                    }
                }

                s = right.PreText;
                if (item.PreText != s) {
                    if (s == "大" || s == "首") {
                        result[col, 8] = item.D ?? string.Empty;
                    } else if (s == "中" || s == "次") {
                        result[col, 9] = item.Z ?? string.Empty;
                    } else if (s == "小" || s == "末") {
                        result[col, 10] = item.X ?? string.Empty;
                    }
                }

                string cellValue = string.Empty;
                for (int index = 8; index < 11; index++) {
                    cellValue += result[col, index];
                }
                result[col, 7] = cellValue.MySort();

                // 与预测号同＋邻
                for (int index = 0; index < 4; index++) {
                    var temp = result[col, 3 + index]
                       + result[col, 7 + index];
                    result[col, 11 + index] = temp.MySort();
                }

                // 计算场次合计
                for (int iRow = 0; iRow < result.RowCount; iRow++) {
                    int count = 0;
                    for (int iCol = 0; iCol < result.ColCount - 1; iCol++) {
                        if (!string.IsNullOrEmpty(result[iCol, iRow]))
                            count++;
                    }

                    result[14, iRow] = count.ToString();

                }
            }

            return result;
        }
    }
}
